//
//  WFPopThreeView.m
//  Cunli
//
//  Created by 湛亚 on 15/11/13.
//  Copyright (c) 2015年 lilubin. All rights reserved.
//

#import "WFPopThreeView.h"

//#define kXHALbumOperationViewSize CGSizeMake(180, 30)
#define kXHAlbumSpatorY 5

@interface WFPopThreeView ()

@property (nonatomic, strong) UIButton *reportButton;
@property (nonatomic, strong) UIButton *likeButton;
@property (nonatomic, strong) UIButton *delectButton;
@property (nonatomic,assign) BOOL isManager;
@property (nonatomic, assign) CGRect targetRect;
@property (nonatomic, assign) CGFloat width;
@end

@implementation WFPopThreeView

+ (instancetype)initailzerWFOperationView {
    WFPopThreeView *operationView = [[WFPopThreeView alloc] initWithFrame:CGRectZero];
    return operationView;
}

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        _isManager=[self isLiaisonMan];
        if (_isManager) {
            _width=180;
        }else{
            _width=120;
        }
        
        self.backgroundColor = [UIColor colorWithWhite:0.000 alpha:0.800];
        self.layer.masksToBounds = YES;
        self.layer.cornerRadius = 5.0;
        [self addSubview:self.likeButton];
        [self addSubview:self.reportButton];
        if (_isManager) {
            [self addSubview:self.delectButton];
        }
        
        
    }
    return self;
}

#pragma mark - Action

- (void)operationDidClicked:(UIButton *)sender {
    [self dismiss];
    if (self.didSelectedOperationCompletion) {
        self.didSelectedOperationCompletion(sender.tag);
    }
}

#pragma mark - Propertys

- (UIButton *)likeButton {
    if (!_likeButton) {
        _likeButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _likeButton.tag = 0;
        [_likeButton addTarget:self action:@selector(operationDidClicked:) forControlEvents:UIControlEventTouchUpInside];
        _likeButton.frame = CGRectMake(0, 0, 60, 30);
        [_likeButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _likeButton.titleLabel.font = [UIFont systemFontOfSize:14];
    }
    return _likeButton;
}

- (UIButton *)reportButton {
    if (!_reportButton) {
        _reportButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _reportButton.tag = 1;
        [_reportButton addTarget:self action:@selector(operationDidClicked:) forControlEvents:UIControlEventTouchUpInside];
        _reportButton.frame = CGRectMake(CGRectGetMaxX(_likeButton.frame), 0, CGRectGetWidth(_likeButton.bounds), CGRectGetHeight(_likeButton.bounds));
        [_reportButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _reportButton.titleLabel.font = [UIFont systemFontOfSize:14];
    }
    return _reportButton;
}

- (UIButton *)delectButton {
    if (!_delectButton) {
        _delectButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _delectButton.tag = 2;
        [_delectButton addTarget:self action:@selector(operationDidClicked:) forControlEvents:UIControlEventTouchUpInside];
        _delectButton.frame = CGRectMake(CGRectGetMaxX(_reportButton.frame), 0, CGRectGetWidth(_reportButton.bounds), CGRectGetHeight(_reportButton.bounds));
        [_delectButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _delectButton.titleLabel.font = [UIFont systemFontOfSize:14];
    }
    return _delectButton;
}

#pragma mark - 公开方法

- (void)showAtView:(UITableView *)containerView rect:(CGRect)targetRect isFavour:(NSString *)zanCount {
    self.targetRect = targetRect;
    if (self.shouldShowed) {
        return;
    }
    
    [containerView addSubview:self];
    
    //CGFloat width = kXHALbumOperationViewSize.width;
    //CGFloat height = kXHALbumOperationViewSize.height;
    
    self.frame = CGRectMake(targetRect.origin.x, targetRect.origin.y - kXHAlbumSpatorY, 0, 30);
    self.shouldShowed = YES;
    
    [UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
        self.frame = CGRectMake(targetRect.origin.x - _width, targetRect.origin.y - kXHAlbumSpatorY, _width, 30);
    } completion:^(BOOL finished) {
        NSString *zanCountString=[NSString stringWithFormat:@"赞%@",zanCount];
        [_likeButton setTitle:zanCountString forState:UIControlStateNormal];
        [_reportButton setTitle:@"举报" forState:UIControlStateNormal];
        [_delectButton setTitle:@"删除" forState:UIControlStateNormal];
    }];
}

- (void)dismiss {
    if (!self.shouldShowed) {
        return;
    }
    
    self.shouldShowed = NO;
    
    [UIView animateWithDuration:0.25f animations:^{
        [_likeButton setTitle:nil forState:UIControlStateNormal];
        [_reportButton setTitle:nil forState:UIControlStateNormal];
        [_delectButton setTitle:nil forState:UIControlStateNormal];
        //CGFloat height = kXHALbumOperationViewSize.height;
        self.frame = CGRectMake(self.targetRect.origin.x, self.targetRect.origin.y - kXHAlbumSpatorY, 0, 30);
    } completion:^(BOOL finished) {
        
        [self removeFromSuperview];
        
    }];
}

#pragma mark - 判断是否是联络员

-(BOOL)isLiaisonMan{
    
    NSDictionary *user=[[NSUserDefaults standardUserDefaults]objectForKey:@"userinfo"];
    NSString *userRoleString=[user objectForKey:@"groupid"];
    if ([userRoleString isEqualToString:@"9"]) {
        return YES;
    }else{
        return NO;
    }
}

@end
